package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.service.MarketIssueService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class MarketIssueServiceImpl implements MarketIssueService {
    @Override
    public List<MarketIssue> list(String page, String limit, String sort, String order, String question) {
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        // 【判空
        if (!StringUtils.isEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        //

        // 写死page,limit
        // page="1";
        // limit="20";
        marketIssueExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);
        session.commit();
        session.close();
        return marketIssues;
    }

    /**
     * 创建问题（市场问题）的方法。
     * 该方法用于将给定的市场问题对象存储到数据库中。
     *
     * @param
     */
    public MarketIssue createIssue(String question, String answer) {
        // 获取数据库会话对象，使用MyBatisUtil工具类。
        SqlSession session = MybatisUtil.getSession();

        // 获取MarketIssueMapper映射器对象，用于执行数据库操作。
        MarketIssueMapper marketIssueMapper = session.getMapper(MarketIssueMapper.class);

        MarketIssue marketIssue = new MarketIssue();

        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);

        // 调用insertSelective方法，将市场问题对象插入数据库中，只插入非空字段。
        marketIssueMapper.insertSelective(marketIssue);

        // 提交事务，将数据持久化到数据库中。
        session.commit();

        // 关闭数据库会话，释放资源。
        session.close();

        return marketIssue;
    }

    @Override
    public int deleteIssue(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        // criteria就是where
        criteria.andIdEqualTo(Integer.parseInt(id));// 这里是
        // delete * from market_issue where id=?
        try {
            mapper.deleteByExample(marketIssueExample);
            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
            return -1;// 失败
        } finally {
            session.close();
        }
        return 0;// 成功
    }

    @Override
    public MarketIssue updateIssue(Integer id, String question, String answer, String addTime, String updateTime, Boolean deleted) {
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper marketIssueMapper = session.getMapper(MarketIssueMapper.class);

        // 我直接把对象拿过来，修改对象里的值
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(id);
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setUpdateTime(LocalDateTime.now());
         // marketIssue.setAddTime(addTime);
        // update question,answer from
        marketIssue.setDeleted(false);

        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();
        criteria.andIdEqualTo(id);
        // 左参数是对象，右参数是你的条件(锁定哪一行)
        marketIssueMapper.updateByExampleSelective(marketIssue,marketIssueExample);

        session.commit();
        session.close();

        return marketIssue;
    }
}
